/**
 * TAC AgentWare http://www.sics.se/tac tac-dev@sics.se
 *
 * Copyright (c) 2001-2005 SICS AB. All rights reserved.
 *
 * SICS grants you the right to use, modify, and redistribute this software for
 * noncommercial purposes, on the conditions that you: (1) retain the original
 * headers, including the copyright notice and this text, (2) clearly document
 * the difference between any derived software and the original, and (3)
 * acknowledge your use of this software in pertaining publications and reports.
 * SICS provides this software "as is", without any warranty of any kind. IN NO
 * EVENT SHALL SICS BE LIABLE FOR ANY DIRECT, SPECIAL OR INDIRECT, PUNITIVE,
 * INCIDENTAL OR CONSEQUENTIAL LOSSES OR DAMAGES ARISING OUT OF THE USE OF THE
 * SOFTWARE.
 *
 * -----------------------------------------------------------------
 *
 * Author : Joakim Eriksson, Niclas Finne, Sverker Janson Created : 23 April,
 * 2002 Updated : $Date: 2005/06/07 19:06:16 $ $Revision: 1.1 $
 * --------------------------------------------------------- DummyAgent is a
 * simplest possible agent for TAC. It uses the TACAgent agent ware to interact
 * with the TAC server.
 *
 * Important methods in TACAgent:
 *
 * Retrieving information about the current Game
 * --------------------------------------------- int getGameID() - returns the
 * id of current game or -1 if no game is currently plaing
 *
 * getServerTime() - returns the current server time in milliseconds
 *
 * getGameTime() - returns the time from start of game in milliseconds
 *
 * getGameTimeLeft() - returns the time left in the game in milliseconds
 *
 * getGameLength() - returns the game length in milliseconds
 *
 * int getAuctionNo() - returns the number of auctions in TAC
 *
 * int getClientPreference(int client, int type) - returns the clients
 * preference for the specified type (types are TACAgent.{ARRIVAL, DEPARTURE,
 * HOTEL_VALUE, E1, E2, E3}
 *
 * int getAuctionFor(int category, int type, int day) - returns the auction-id
 * for the requested resource (categories are TACAgent.{CAT_FLIGHT, CAT_HOTEL,
 * CAT_ENTERTAINMENT and types are TACAgent.TYPE_INFLIGHT,
 * TACAgent.TYPE_OUTFLIGHT, etc)
 *
 * int getAuctionCategory(int auction) - returns the category for this auction
 * (CAT_FLIGHT, CAT_HOTEL, CAT_ENTERTAINMENT)
 *
 * int getAuctionDay(int auction) - returns the day for this auction.
 *
 * int getAuctionType(int auction) - returns the type for this auction
 * (TYPE_INFLIGHT, TYPE_OUTFLIGHT, etc).
 *
 * int getOwn(int auction) - returns the number of items that the agent own for
 * this auction
 *
 * Submitting Bids --------------------------------------------- void
 * submitBid(Bid) - submits a bid to the tac server
 *
 * void replaceBid(OldBid, Bid) - replaces the old bid (the current active bid)
 * in the tac server
 *
 * Bids have the following important methods: - create a bid with new
 * Bid(AuctionID)
 *
 * void addBidPoint(int quantity, float price) - adds a bid point in the bid
 *
 * Help methods for remembering what to buy for each auction:
 * ---------------------------------------------------------- int
 * getAllocation(int auctionID) - returns the allocation set for this auction
 * void setAllocation(int auctionID, int quantity) - set the allocation for this
 * auction
 *
 *
 * Callbacks from the TACAgent (caused via interaction with server)
 *
 * bidUpdated(Bid bid) - there are TACAgent have received an answer on a bid
 * query/submission (new information about the bid is available) bidRejected(Bid
 * bid) - the bid has been rejected (reason is bid.getRejectReason())
 * bidError(Bid bid, int error) - the bid contained errors (error represent
 * error status - commandStatus)
 *
 * quoteUpdated(Quote quote) - new information about the quotes on the auction
 * (quote.getAuction()) has arrived quoteUpdated(int category) - new information
 * about the quotes on all auctions for the auction category has arrived (quotes
 * for a specific type of auctions are often requested at once).
 *
 * auctionClosed(int auction) - the auction with id "auction" has closed
 *
 * transaction(Transaction transaction) - there has been a transaction
 *
 * gameStarted() - a TAC game has started, and all information about the game is
 * available (preferences etc).
 *
 * gameStopped() - the current game has ended
 *
 */
package se.sics.tac.aw;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import se.sics.tac.util.ArgEnumerator;
import java.util.logging.*;

public class DummyAgent extends AgentImpl {

    private static final Logger log =
            Logger.getLogger(DummyAgent.class.getName());
    private static final boolean DEBUG = false;
    
    private int[][] entertainments;           

    private class ClientData {
        
        int ArrivalPref;
        int DeparturePref;
        /**
         * 50-150
         */
        int HotelPremium;
        /*
         * 0-200 for every ent type
         */
        Map<Integer, Integer> EntPremium;
        
        class ClientAllocations
        {
            // Hotel
            Boolean goodHotel;
            Boolean[] hasHotelAtDay;
            
            // Flight
            Boolean hasInFlight;
            int InFlightDay;
            Boolean hasOutFlight;
            int OutFlightDay;
            
            // Ent
            // TODO, amit akarsz tárolni a hozzárendelt entertainmentekről pl. [4][3]-as tömb intekkel, -1 ,0,1,2 értékkekkel, asszerint, hogy aznap mit csinál
            // -1 a semmi
            int [][] entertainment;

            public ClientAllocations() {
                /*
                 * Mert 4 éjszaka lehet foglalni 5 napra
                 */
                hasHotelAtDay = new Boolean[4];  
                
                for(int i=0; i<4; ++i)
                {
                    hasHotelAtDay[i] = false;     
                }
                
                hasInFlight = false;
                hasOutFlight = false;
                
                // TODO inicializáld is amit ide ***tál! :D
                // jól oda ****tam :D
                entertainment = new int[4][3];               
                
            }
        }
        
        ClientAllocations Allocations;
        
        ClientData()
        {
            EntPremium = new HashMap();
            
            Allocations = new ClientData.ClientAllocations();
        }
    }
    
    private ClientData[] Clients;
    private float[] Prices;
            
    private int PremiumHotelLimit = 30;
    
    protected void init(ArgEnumerator args) {
    }

    public void quoteUpdated(Quote quote) {
        int auction = quote.getAuction();
        int auctionCategory = agent.getAuctionCategory(auction);

        switch(auctionCategory)
        {
            case TACAgent.CAT_HOTEL:
            {
                int alloc = agent.getAllocation(auction);
                if (alloc > 0 && quote.hasHQW(agent.getBid(auction))
                        && quote.getHQW() < alloc) {
                    int own = agent.getOwn(auction);
                    Bid bid = new Bid(auction);
                    Prices[auction] = quote.getAskPrice()*1.07f;
                    // Can not own anything in hotel auctions...
                    bid.addBidPoint(alloc-own, Prices[auction]);
                    log.fine("új bid a " + auction + " aukcióra(hotel): ("+(alloc-own)+", " + Prices[auction] + ")");

                    log.finest("submitting bid with alloc="
                        + agent.getAllocation(auction)
                        + " own=" + agent.getOwn(auction));
                    agent.submitBid(bid);
                }
            }
                break;
            case TACAgent.CAT_FLIGHT:
                break;
            case TACAgent.CAT_ENTERTAINMENT:
                break;
        }
    }
    
    private int getClientDistance(int i)
    {
        int s = 0;
        for(int j=Clients[i].ArrivalPref; j<Clients[i].DeparturePref; ++j)
        {
            if(!Clients[i].Allocations.hasHotelAtDay[j-1])
            {
                ++s;
            }
            if(!Clients[i].Allocations.hasInFlight)
                ++s;
            if(!Clients[i].Allocations.hasOutFlight)
                ++s;
        }
        return s;
    }
    
    private void hotelTransaction(int day, int type)
    {
                Boolean found = false;
                int index = -1;
                int score = 5;
                for(int i=0; i<8; ++i)
                {
                    if(Clients[i].Allocations.goodHotel.equals(type == TACAgent.TYPE_GOOD_HOTEL)
                            && Clients[i].Allocations.hasHotelAtDay[day-1].equals(false))
                    {
                        if(!found)
                        {
                            found = true;
                            index = i;
                        }
                        else
                        {
                            int s = getClientDistance(i);
                            if(s<score)
                            {
                                score = s;
                                index = i;
                            }
                        }
                    }
                }
                if(!found)
                {
                    log.warning("Nem találtam meg a hotelhez a vevőt!");
                }
                else
                {
                    Clients[index].Allocations.hasHotelAtDay[day-1] = true;
                    clientUpdated(index, true, false);
                }
    }
    
    private void flightTransaction(int day, int type)
    {
    
        Boolean inf = TACAgent.TYPE_INFLIGHT == type;

        Boolean found = false;
        int index = -1;
        int score = 5;
        for(int i=0; i<8; ++i)
        {
            if( (inf ?
                    (!Clients[i].Allocations.hasInFlight) :
                    (!Clients[i].Allocations.hasOutFlight))
                    &&
                 (inf ?
                    (Clients[i].ArrivalPref == day) :
                    (Clients[i].DeparturePref == day)))
            {
                if(!found)
                {
                    found = true;
                    index = i;
                }
                else
                {
                    int s = getClientDistance(i);
                    if(s<score)
                    {
                        score = s;
                        index = i;
                    }
                }
            }
        }
        if(!found)
        {
            log.warning("Nem találtam meg a repülőhöz a vevőt!");
        }
        else
        {
            if(inf)
            {
                Clients[index].Allocations.hasInFlight = true;
                clientUpdated(index, false, true);
            }
            else
            {
                Clients[index].Allocations.hasOutFlight = true;
                clientUpdated(index, false, true);
            }
        }
    }
    
    public void transaction(Transaction transaction)
    {
        int auction = transaction.getAuction();
        switch(TACAgent.getAuctionCategory(auction))
        {
            case TACAgent.CAT_HOTEL:
            {
                log.fine("új tranzakció az akción "
                    + transaction.getAuction()
                    + "(hotel) (" 
                    + transaction.getQuantity()
                    + ", "
                    + transaction.getPrice()
                    + ")");
                int day = TACAgent.getAuctionDay(auction);
                int type =TACAgent.getAuctionType(auction);
                hotelTransaction(day, type);
            }
                break;
            case TACAgent.CAT_FLIGHT:
            {
                log.fine("új tranzakció az akción "
                    + transaction.getAuction()
                    + "(repülő) (" 
                    + transaction.getQuantity()
                    + ", "
                    + transaction.getPrice()
                    + ")");
                
                int day = TACAgent.getAuctionDay(auction);
                int type =TACAgent.getAuctionType(auction);
                flightTransaction(day, type);
            }
                break;
            case TACAgent.CAT_ENTERTAINMENT:
                break;
        }
    }

    public void quoteUpdated(int auctionCategory) {
        if(auctionCategory == TACAgent.CAT_FLIGHT)
        {
            for(int auction=0; auction < TACAgent.getAuctionNo(); ++auction)
            {
                if(TACAgent.getAuctionCategory(auction) != TACAgent.CAT_FLIGHT)
                    continue;
                
                int alloc = agent.getAllocation(auction);
                int own = agent.getOwn(auction);
                
                if (alloc > 0 && own < alloc) {
                    if(agent.getGameTimeLeft() < 50000)
                    {
                        Bid bid = new Bid(auction);
                        Prices[auction] = 10000;
                        bid.addBidPoint(alloc-own, Prices[auction]);
                        try
                        {
                            agent.replaceBid(agent.getBid(auction), bid);
                            log.fine("új bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + Prices[auction] + ")");
                        }
                        catch(IllegalStateException e)
                        {
                            log.warning("Bid csere hiba (repülő): " + e.getMessage());
                        }
                    }
                    else
                    {
                        float price = 270 + (400-270)*(agent.getGameTime()/1000 - 2*60)/(7*60);
                        if(price > Prices[auction]+5)
                        {
                            Bid bid = new Bid(auction);
                            Prices[auction] = price;
                            bid.addBidPoint(alloc-own, Prices[auction]);
                            try
                            {
                                agent.replaceBid(agent.getBid(auction), bid);
                                log.fine("új bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + Prices[auction] + ")");
                            }
                            catch(IllegalStateException e)
                            {
                                log.warning("Bid csere hiba (repülő): " + e.getMessage());
                            }
                        }
                    }
                }
            }
        }
        log.fine("All quotes for "
                + agent.auctionCategoryToString(auctionCategory)
                + " has been updated");
    }

    public void bidUpdated(Bid bid) {
        log.fine("Bid Updated: id=" + bid.getID() + " auction="
                + bid.getAuction() + " state="
                + bid.getProcessingStateAsString());
        log.fine("       Hash: " + bid.getBidHash());
    }

    public void bidRejected(Bid bid) {
        log.warning("Bid Rejected: " + bid.getID());
        log.warning("      Reason: " + bid.getRejectReason()
                + " (" + bid.getRejectReasonAsString() + ')');
    }

    public void bidError(Bid bid, int status) {
        log.warning("Bid Error in auction " + bid.getAuction() + ": " + status
                + " (" + agent.commandStatusToString(status) + ')');
    }

    public void gameStarted() {
        log.fine("Game " + agent.getGameID() + " started!");

        Prices = new float[agent.getAuctionNo()];
        
        Clients = new ClientData[8];
        
        entertainments = new int[4][3];
                
        for(int i=0; i<4; ++i)
        {
            int auction = TACAgent.getAuctionFor(TACAgent.CAT_ENTERTAINMENT, TACAgent.TYPE_ALLIGATOR_WRESTLING, i+1);
            entertainments[i][0] = agent.getOwn(auction);
            auction = TACAgent.getAuctionFor(TACAgent.CAT_ENTERTAINMENT, TACAgent.TYPE_AMUSEMENT, i+1);
            entertainments[i][1] = agent.getOwn(auction);
            auction = TACAgent.getAuctionFor(TACAgent.CAT_ENTERTAINMENT, TACAgent.TYPE_MUSEUM, i+1);
            entertainments[i][2] = agent.getOwn(auction);                    
        }
        
        getClientData();
        startBid();
    }

    public void gameStopped() {
        log.fine("Game Stopped!");
    }

    public void auctionClosed(int auction) {
        log.fine("*** Aukció " + auction + " bezárt!");
        
        if(TACAgent.getAuctionCategory(auction) == TACAgent.CAT_HOTEL)
        {
            int own = agent.getOwn(auction);
            int alloc = agent.getAllocation(auction);
log.fine("AJAJ");
            if(own < alloc)
            {
                int day = TACAgent.getAuctionDay(auction);
                int type = TACAgent.getAuctionType(auction);
                
                int[] distance = new int[8];
                int[] sort = new int[8];
                for(int i = 0; i<8; ++i)
                {
                    sort[i] = i;
                    distance[i] = getClientDistance(i);
                }
                for(int i = 0; i<7; ++i)
                {
                    for(int j = i; j<8; ++j)
                    {
                        if(distance[sort[i]] < distance[sort[j]] )
                        {
                            int temp = sort[i];
                            sort[i]=sort[j];
                            sort[j] = temp;
                        }
                    }
                }
                // egy klienst át kell ütemeznünk
                int to_be_solved = 0;
                for(int i = alloc - own; i>0; --i)
                {
log.fine("distance:"+distance[sort[to_be_solved]]);
                    if(Clients[sort[to_be_solved]].DeparturePref == day && 
                            day - 1 - (Clients[sort[to_be_solved]].Allocations.hasInFlight ? Clients[sort[to_be_solved]].Allocations.InFlightDay : Clients[sort[to_be_solved]].ArrivalPref) > 0)
                    {
log.fine("dep = day");
                        // gyorsan veszünk neki egy repjegye
                        int old_auct = TACAgent.getAuctionFor(TACAgent.CAT_FLIGHT, TACAgent.TYPE_OUTFLIGHT, day);
                        int old_alloc = agent.getAllocation(old_auct);
                        int old_own = agent.getOwn(old_auct);
                        if(Clients[sort[to_be_solved]].Allocations.hasOutFlight)
                        {
log.fine("van jegye");
                            // de van neki már egy dfeleseleges
                            if(old_alloc > old_own)
                            {
                                Bid old_bid = new Bid(old_auct);
                                agent.setAllocation(old_auct, old_alloc-1);
                                old_bid.addBidPoint(old_alloc-1, Prices[old_auct]);
                                // decrement bid
                                try
                                {
log.fine("kísérlet bid csere " + old_auct + " (" + (old_alloc-1) +", " + Prices[old_auct]);
                                    agent.replaceBid(agent.getBid(auction), old_bid);
log.fine("sikeres bid csere " + old_auct + " (" + (old_alloc-1) +", " + Prices[old_auct]);
                                }
                                catch(Exception e)
                                {
                                    log.warning(e.getMessage());
                                }
                                flightTransaction(day, TACAgent.TYPE_OUTFLIGHT);
                            }
                        }
                        int fauct = TACAgent.getAuctionFor(TACAgent.CAT_FLIGHT, TACAgent.TYPE_OUTFLIGHT, day-1);
                        Bid bid;
                        try
                        {
                            bid= agent.getBid(fauct);
                        }
                        catch(Exception e)
                        {
                            bid = new Bid(fauct);
                        }
                        bid.addBidPoint(1, 10000);
                        try
                        {
                            log.fine("kísérlet csere bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + 10000 + ")");
                            agent.replaceBid(agent.getBid(auction), bid);
                            log.fine("sikeres csre bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + 10000 + ")");
                        }
                        catch(IllegalStateException e)
                        {
                            try
                            {
                            log.fine("kísérlet új bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + 10000 + ")");
                                agent.submitBid(bid);
                            }
                            catch(Exception e1)
                            {
                                log.warning("Bid csere hiba (repülő): " + e.getMessage());
                            }
                        }
                    }
                    
                    
                    else if(Clients[sort[to_be_solved]].ArrivalPref == day && 
                            (Clients[sort[to_be_solved]].Allocations.hasOutFlight ? Clients[sort[to_be_solved]].Allocations.OutFlightDay : Clients[sort[to_be_solved]].DeparturePref)- day - 1 > 0)
                    {
log.fine("arr = day");
                        // gyorsan veszünk neki egy repjegye
                        int old_auct = TACAgent.getAuctionFor(TACAgent.CAT_FLIGHT, TACAgent.TYPE_INFLIGHT, day);
                        int old_alloc = agent.getAllocation(old_auct);
                        int old_own = agent.getOwn(old_auct);
                        if(Clients[sort[to_be_solved]].Allocations.hasInFlight)
                        {
log.fine("van jegye");
                            // de van neki már egy dfeleseleges
                            if(old_alloc > old_own)
                            {
                                Bid old_bid = new Bid(old_auct);
                                agent.setAllocation(old_auct, old_alloc-1);
                                old_bid.addBidPoint(old_alloc-1, Prices[old_auct]);
                                // decrement bid
                                try
                                {
log.fine("kísérlet bid csere " + old_auct + " (" + (old_alloc-1) +", " + Prices[old_auct]);
                                    agent.replaceBid(agent.getBid(auction), old_bid);
log.fine("sikeres bid csere " + old_auct + " (" + (old_alloc-1) +", " + Prices[old_auct]);
                                }
                                catch(Exception e)
                                {
                                    log.warning(e.getMessage());
                                }
                                flightTransaction(day, TACAgent.TYPE_INFLIGHT);
                            }
                        }
                        int fauct = TACAgent.getAuctionFor(TACAgent.CAT_FLIGHT, TACAgent.TYPE_INFLIGHT, day-1);
                        Bid bid;
                        try
                        {
                            bid= agent.getBid(fauct);
                        }
                        catch(Exception e)
                        {
                            bid = new Bid(fauct);
                        }
                        bid.addBidPoint(1, 10000);
                        try
                        {
                            log.fine("kísérlet csere bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + 10000 + ")");
                            agent.replaceBid(agent.getBid(auction), bid);
                            agent.replaceBid(agent.getBid(auction), bid);
                            log.fine("sikeres csre bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + 10000 + ")");
                        }
                        catch(IllegalStateException e)
                        {
                            try
                            {
                            log.fine("kísérlet új bid a " + auction + " aukcióra(repülő): ("+(alloc-own)+", " + 10000 + ")");
                                agent.submitBid(bid);
                            }
                            catch(Exception e1)
                            {
                                log.warning("Bid csere hiba (repülő): " + e.getMessage());
                            }
                        }
                    }
                    
                    // TODO a holtelt is rakjuk át
                    
                    ++to_be_solved;
                }
            }
        }
    }
    
    private void clientUpdated(int clientID, Boolean hotelUpdated, Boolean flightUpdated)
    {
        // TODO Tomi! Itt tudod meg, hogy vettem hotelszobát legalább egy napra. De neked kell lecsekkolnod, hogy teljes-e már a nyaralása.
        // Ha nem tudtam neki holtet venni úgy, ahogy ő akarta, akkor a az allocations-ben nem a preferált dátum fog szerepelni
    }
    
    private void buyEntertainmentTickets()
    {
        for (int i=0; i<8; ++i){
            if (Clients[i].Allocations.hasInFlight && Clients[i].Allocations.hasOutFlight)
            {
                // 1-4
                int firstDay = Clients[i].Allocations.InFlightDay;
                int lastDay = Clients[i].Allocations.OutFlightDay;
                
                boolean hasHotelNights = true;
                
                for (int j = firstDay; j<lastDay; ++j) {
                    if (!Clients[i].Allocations.hasHotelAtDay[j-1]) {
                        hasHotelNights = false;
                    }
                }
                
                if (hasHotelNights)
                {
                    //aligator
                    int alligatorPremium = Clients[i].EntPremium.get(TACAgent.E1);
                    int amusementPremium = Clients[i].EntPremium.get(TACAgent.E2);
                    int museumPremium = Clients[i].EntPremium.get(TACAgent.E3);
                        
                    int[] sortedPremium = new int[3];
                    sortedPremium[0] = alligatorPremium;
                    sortedPremium[1] = amusementPremium;
                    sortedPremium[2] = museumPremium;
                    
                    Arrays.sort(sortedPremium);
                    
                    
                    for (int j = firstDay; j<lastDay; ++j) { 
                        
                        if (sortedPremium[2] == alligatorPremium) {
                            if (entertainments[j-1][0] > 0){
                                entertainments[j-1][0]--;
                                Clients[i].Allocations.entertainment[j-1][0] = 1;
                            } else {
                                if (sortedPremium[1] == amusementPremium){
                                    if (entertainments[j-1][1] > 0){
                                        entertainments[j-1][1]--;
                                        Clients[i].Allocations.entertainment[j-1][1] = 1;
                                    } else {
                                        if (entertainments[j-1][2] > 0) {
                                            entertainments[j-1][2]--;
                                            Clients[i].Allocations.entertainment[j-1][2] = 1;
                                        }
                                    }
                                } else {
                                    if (entertainments[j-1][2] > 0){
                                        entertainments[j-1][2]--;
                                        Clients[i].Allocations.entertainment[j-1][2] = 1;
                                    } else {
                                        if (entertainments[j-1][1] > 0){
                                            entertainments[j-1][1]--;
                                            Clients[i].Allocations.entertainment[j-1][1] = 1;
                                        }
                                    }
                                }
                            }
                        } else if (sortedPremium[2] == amusementPremium){
                             if (entertainments[j-1][1] > 0){
                                entertainments[j-1][1]--;
                                Clients[i].Allocations.entertainment[j-1][1] = 1;
                            } else {
                                if (sortedPremium[1] == alligatorPremium){
                                    if (entertainments[j-1][0] > 0){
                                        entertainments[j-1][0]--;
                                        Clients[i].Allocations.entertainment[j-1][0] = 1;
                                    } else {
                                        if (entertainments[j-1][2] > 0) {
                                            entertainments[j-1][2]--;
                                            Clients[i].Allocations.entertainment[j-1][2] = 1;
                                        }
                                    }
                                } else {
                                    if (entertainments[j-1][2] > 0){
                                        entertainments[j-1][2]--;
                                        Clients[i].Allocations.entertainment[j-1][2] = 1;
                                    } else {
                                        if (entertainments[j-1][0] > 0){
                                            entertainments[j-1][0]--;
                                            Clients[i].Allocations.entertainment[j-1][0] = 1;
                                        }
                                    }
                                }
                            }
                        } else {
                            if (entertainments[j-1][2] > 0){
                                entertainments[j-1][2]--;
                                Clients[i].Allocations.entertainment[j-1][2] = 1;
                            } else {
                                if (sortedPremium[1] == amusementPremium){
                                    if (entertainments[j-1][1] > 0){
                                        entertainments[j-1][1]--;
                                        Clients[i].Allocations.entertainment[j-1][1] = 1;
                                    } else {
                                        if (entertainments[j-1][0] > 0) {
                                            entertainments[j-1][0]--;
                                            Clients[i].Allocations.entertainment[j-1][0] = 1;
                                        }
                                    }
                                } else {
                                    if (entertainments[j-1][0] > 0){
                                        entertainments[j-1][0]--;
                                        Clients[i].Allocations.entertainment[j-1][0] = 1;
                                    } else {
                                        if (entertainments[j-1][1] > 0){
                                            entertainments[j-1][1]--;
                                            Clients[i].Allocations.entertainment[j-1][1] = 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    
                }
                
            }
               
        }
    }

    private void getClientData() {
        Clients = new ClientData[8];
        for (int i = 0; i < 8; ++i) {
            Clients[i] = new ClientData();
            log.fine(i+". kliens ----------------------------------------------------");
            
            Clients[i].ArrivalPref = agent.getClientPreference(i, TACAgent.ARRIVAL);
            log.fine("érkezés: "+Clients[i].ArrivalPref);
            Clients[i].DeparturePref = agent.getClientPreference(i, TACAgent.DEPARTURE);
            log.fine("indulás: "+Clients[i].DeparturePref);
            
            int infauction = TACAgent.getAuctionFor(TACAgent.CAT_FLIGHT, TACAgent.TYPE_INFLIGHT, Clients[i].ArrivalPref);
            agent.setAllocation(infauction, agent.getAllocation(infauction) + 1);
            int outfauction = TACAgent.getAuctionFor(TACAgent.CAT_FLIGHT, TACAgent.TYPE_OUTFLIGHT, Clients[i].DeparturePref);
            agent.setAllocation(outfauction, agent.getAllocation(outfauction) + 1);
            
            Clients[i].HotelPremium = agent.getClientPreference(i, TACAgent.HOTEL_VALUE);
            log.fine("hotel jutalom: "+Clients[i].HotelPremium);
            
            Clients[i].EntPremium.put(TACAgent.E1, agent.getClientPreference(i, TACAgent.E1));
            log.fine("e1 jutalom: "+Clients[i].EntPremium.get(TACAgent.E1));
            Clients[i].EntPremium.put(TACAgent.E2, agent.getClientPreference(i, TACAgent.E2));
            log.fine("e2 jutalom: "+Clients[i].EntPremium.get(TACAgent.E2));
            Clients[i].EntPremium.put(TACAgent.E3, agent.getClientPreference(i, TACAgent.E3));
            log.fine("e3 jutalom: "+Clients[i].EntPremium.get(TACAgent.E3));
            log.fine("--------------------------------------------------------------");
        }
        
        int[] tomb = new int[8];
        for (int i = 0; i < 8; ++i) {
            tomb[i] = i;
        }
        
        for (int i = 0; i < 7; ++i) {
            for (int j = i+1; j < 8; ++j) {
                if(Clients[tomb[i]].HotelPremium < Clients[tomb[j]].HotelPremium)
                {
                    int temp = tomb[i];
                    tomb[i] = tomb[j];
                    tomb[j] = temp;
                }
            }
        }
        
        for (int i = 0; i < 8; ++i) {
            Clients[tomb[i]].Allocations.goodHotel = i < 4;
            int holteltype = Clients[tomb[i]].Allocations.goodHotel ? TACAgent.TYPE_GOOD_HOTEL : TACAgent.TYPE_CHEAP_HOTEL;
            log.fine(tomb[i] +". kliens " +(Clients[tomb[i]].Allocations.goodHotel ? "jó" : "olcsó") + " hotelt kap majd.");
            
            for(int day = Clients[tomb[i]].ArrivalPref; day < Clients[tomb[i]].DeparturePref; ++day)
            {
                int auction = TACAgent.getAuctionFor(TACAgent.CAT_HOTEL, holteltype, day);
                agent.setAllocation(auction, agent.getAllocation(auction) + 1);
            }
        }
    }
    
    private void startBid()
    {
        for(int auction=0; auction<agent.getAuctionNo(); ++auction)
        {
            int alloc = agent.getAllocation(auction);
            int own = agent.getOwn(auction);
            
            Bid bid = new Bid(auction);
            
            switch(TACAgent.getAuctionCategory(auction))
            {
                case TACAgent.CAT_FLIGHT:   // Bid for flights
                    if(alloc > 0)
                    {
                        Prices[auction] = 300;
                        bid.addBidPoint(alloc, Prices[auction]);
                        agent.submitBid(bid);
                        log.fine("új bid a " + auction + " aukcióra(repülő): ("+alloc+", " + Prices[auction] + ")");
                    }
                    break;
                case TACAgent.CAT_HOTEL:    // Bid for hotel
                    if(alloc > 0)
                    {
                        Prices[auction] = 10;
                        bid.addBidPoint(alloc, Prices[auction]);
                        agent.submitBid(bid);
                        log.fine("új bid a " + auction + " aukcióra(hotel): ("+alloc+", " + Prices[auction] + ")");
                    }
                    break;
                case TACAgent.CAT_ENTERTAINMENT:
                    // TODO Tomi! 
                    break;
            }            
        }
    }


    // -------------------------------------------------------------------
    // Only for backward compability
    // -------------------------------------------------------------------
    public static void main(String[] args) {
        TACAgent.main(args);
    }
} // DummyAgent
